//
//  OpinionViewController.m
//  ksjv2
//
//  Created by mac on 14/12/16.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//
//---------------------------------------------------------
//                    *              *
//                  *      意见界面     *
//                    *              *
//---------------------------------------------------------
#import "OpinionViewController.h"
#import "Util.h"
@interface OpinionViewController (){
    UILabel*textViewPlaceholder;
    NSMutableArray*buttons;
    UILabel*buttonLine;
    
}
@property(strong,nonatomic)NSString*index;
@end

@implementation OpinionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initbuttons];
    [self initPlaceholder];
    [self initRightBarButton];
    // Do any additional setup after loading the view.
}
-(void)initbuttons{
    self.index=@"0";
    buttons=[[NSMutableArray alloc]init];
    float size = self.view.frame.size.width/2;
    for (int i =0; i<2; i++) {
        UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(size*i, 25, size, 40)];
        if (i) {
            [button setTitle:@"产品建议" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
        }else{
            [button setTitle:@"订购问题" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.view addSubview:button];
        [buttons addObject:button];
    }
    buttonLine=[[UILabel alloc]initWithFrame:CGRectMake(30,60, size/2+15, 1)];
//    buttonLine.text=@"__________________";
    buttonLine.textAlignment=NSTextAlignmentCenter;
    buttonLine.textColor=[UIColor redColor];
    buttonLine.backgroundColor=[UIColor redColor];
    [self.view addSubview:buttonLine];
}
-(void)opinionInWWW:(int)tag{
    [self buttonAction:buttons[tag]];
}
-(void)buttonAction:(id)obj{
    UIButton*but=(UIButton*)obj;
    [self changeColor:[but tag]];
    [UIView animateWithDuration:.2 animations:^{
    float size = self.view.frame.size.width/2-20;
        buttonLine.frame=CGRectMake(size*[but tag]+30+([but tag]*20), 60, buttonLine.frame.size.width, 1);
    }];
}
-(void)changeColor:(NSInteger)tag{
    for (UIButton*buton in buttons) {
        [buton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    UIButton*button=buttons[tag];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.index=[NSString stringWithFormat:@"%ld",tag];
}
-(void)commitData{
    [self resignFirst];

///意见建议
    [SVProgressHUD show];
        if (self.Content.text.length>5) {
            NSString*type;
            if ([self.index intValue]) {
                type=@"0";
            }else{
                type=@"1";
            }
            NSString*email=[[UserDefaults objectForKey:@"user"] objectForKey:@"email"];
            if (email==nil) {
                email=@"";
            }
            NSString*phone=[[UserDefaults objectForKey:@"user"] objectForKey:@"mobile"];
            if (phone==nil) {
                phone=@"";
            }
            [[AppAPIClient sharedClient] POST:@"System/Suggest" parameters:@{@"os":@"1",@"type":type,@"email":email,@"mobile":phone,@"text":self.Content.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//                int code = [responseObject[@"code"] intValue];
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
//                self.email.text=@"";
                self.Content.text=@"";
//                NSLog(@"%@",responseObject);
//                [SVProgressHUD showSuccessWithStatus:LOADING_TEXT_SUCCESS];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
                NSLog(@"Error:%@ => %@",error,operation.responseString);
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:@"留言内容少于5字..."];
        }


}

-(void)viewDidLayoutSubviews{
    

}
-(void)initRightBarButton{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 40, 40)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(commitData) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem= rightItem;
    
}
-(void)initPlaceholder{
    self.title=@"留言";
    textViewPlaceholder=[[UILabel alloc]initWithFrame:CGRectMake(5, 10, 200, 20)];
    textViewPlaceholder.textColor=[UIColor colorWithWhite:0.729 alpha:0.820];
    textViewPlaceholder.text=@"请写下你订购遇到的问题";
    [self.Content addSubview:textViewPlaceholder];
    
    
}
-(void)setIndex:(NSString *)index{
    if (index!=nil) {
        if ([index intValue]) {
            textViewPlaceholder.text=@"请写下你对本产品的建议";
        }else{
            textViewPlaceholder.text=@"请写下你订购遇到的问题";
        }
    }
}


#pragma mark - textView Delegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
    
    if ([text isEqualToString:@"\n"]) {//检测到“完成”
        
        [textView resignFirstResponder];//释放键盘
        
        return NO;
        
    }
    
    if (textView.text.length==0){//textview长度为0
        
        if ([text isEqualToString:@""]) {//判断是否为删除键
            
            textViewPlaceholder.hidden=NO;//隐藏文字
            
        }else{
            
            textViewPlaceholder.hidden=YES;
            
        }
        
    }else{//textview长度不为0
        
        if (textView.text.length==1){//textview长度为1时候
            
            if ([text isEqualToString:@""]) {//判断是否为删除键
                
                textViewPlaceholder.hidden=NO;
                
            }else{//不是删除
                
                textViewPlaceholder.hidden=YES;
                
            }
            
        }else{//长度不为1时候
            
            textViewPlaceholder.hidden=YES;
            
        }
        
    }
    
    return YES;
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self resignFirst];
}
-(void)resignFirst{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
